package com.cblz.bd.server.utils;

import com.cblz.bd.common.core.constant.RedisConstant;
import com.cblz.bd.common.core.exception.ValidateCodeException;
import com.cblz.bd.redis.spring.boot.starter.service.RedisService;
import com.cblz.bd.server.core.entity.properties.CaptchaProperties;
import com.pig4cloud.captcha.GifCaptcha;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * {@code @Name:} BdCaptchaUtil
 * {@code @Description: } 验证码工具类
 * {@code @Author:} by yanjieping
 * {@code @CreateDate:} 2022/11/8 - 周二
 */
@Slf4j
@Component
@RequiredArgsConstructor
@EnableConfigurationProperties(CaptchaProperties.class)
public class BdCaptchaUtil {

    private final RedisService redisService;
    private final CaptchaProperties captchaProperties;

    /**
     * 返回GIF格式的验证码
     *
     * @return
     */
    @SneakyThrows
    public void initCaptcha(HttpServletRequest request, HttpServletResponse response) {
        String key = request.getParameter("key");
        if (StringUtils.isBlank(key)) {
            throw new ValidateCodeException("验证码key不能为空");
        }

        response.setContentType(MediaType.IMAGE_GIF_VALUE);
        response.setHeader(HttpHeaders.PRAGMA, "No-cache");
        response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache");
        response.setDateHeader(HttpHeaders.EXPIRES, 0L);

        // 生成验证码
        GifCaptcha captcha = new GifCaptcha(
                captchaProperties.getWidth(),
                captchaProperties.getHeight(),
                captchaProperties.getDigits());

        // 设置内置字体
        captcha.setFont(captchaProperties.getFontStyle());
        // 设置验证码字符类型
        captcha.setCharType(captchaProperties.getCaptchaCharType());

        redisService.set(RedisConstant.CAPTCHA_PREFIX.getValue() + key, StringUtils.lowerCase(captcha.text()), captchaProperties.getExpiredTime());
        // 返回流
        captcha.out(response.getOutputStream());
    }

    /**
     * 校验验证码
     *
     * @param key   前端上送 key
     * @param value 前端上送待校验值
     */
    public void check(String key, String value) throws ValidateCodeException {
        Object codeInRedis = redisService.get(RedisConstant.CAPTCHA_PREFIX.getValue() + key);
        if (StringUtils.isBlank(value)) {
            throw new ValidateCodeException("请输入验证码");
        }
        if (codeInRedis == null) {
            throw new ValidateCodeException("验证码已过期");
        }
        if (!StringUtils.equalsIgnoreCase(value, String.valueOf(codeInRedis))) {
            throw new ValidateCodeException("验证码不正确");
        }
    }

    /**
     * 删除校验码
     *
     * @param key
     */
    public void delCaptcha(String key) {
        log.info("删除验证码：{}",RedisConstant.CAPTCHA_PREFIX.getValue() + key);
        redisService.del(RedisConstant.CAPTCHA_PREFIX.getValue() + key);
    }
}
